package com.edu.ucenterservice.controller;


import com.edu.commonutils.JwtUtils;
import com.edu.commonutils.R;
import com.edu.commonutils.user.UcenterMemberOrder;
import com.edu.ucenterservice.domain.UcenterMember;
import com.edu.ucenterservice.domain.vo.LoginVo;
import com.edu.ucenterservice.domain.vo.RegisterVo;
import com.edu.ucenterservice.service.UcenterMemberService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 会员表 前端控制器
 * </p>
 *
 * @author ljw
 * @since 2022-09-22
 */
@Api(description = "登陆注册接口")
//@CrossOrigin
@RestController
@RequestMapping("/ucenterservice/ucentermember")
public class UcenterMemberController {


    @Autowired(required = false)
    private UcenterMemberService memberService;

    @ApiOperation(value = "会员登录")
    @PostMapping("/login")
    @ResponseBody
    public R login(@RequestBody LoginVo loginVo) {
        //登录成功 返回jwt生成token值
        String token = memberService.login(loginVo);
        return R.ok().data("token", token);
    }

    @ApiOperation(value = "会员注册")
    @PostMapping("/register")
    public R register(@RequestBody RegisterVo registerVo){
        memberService.register(registerVo);
        return R.ok();
    }

    @ApiOperation("返回用户信息")
    @GetMapping("/getMembeiInfo")
    @ResponseBody
    public R gettoken(HttpServletRequest request){
        //带哦用jwt的方法，根据request对象获取头信息 获取id
        String mennmberid = JwtUtils.getMemberIdByJwtToken(request);
        //根据id查询出账号
        UcenterMember member = memberService.getById(mennmberid);

        return R.ok().data("userInfo",member);
    }

    //根据token字符串获取用户信息
    @PostMapping("/getInfoUc/{id}")
    public UcenterMember getInfo(@PathVariable String id) {
        //根据用户id获取用户信息
        UcenterMember ucenterMember = memberService.getById(id);
        UcenterMember memeber = new UcenterMember();
        BeanUtils.copyProperties(ucenterMember,memeber);
        return memeber;
    }

    //根据token字符串获取用户信息
    @PostMapping("/getInfoOrder/{id}")
    @ResponseBody
    public UcenterMemberOrder getInfoOrder(@PathVariable String id) {
        //根据用户id获取用户信息
        UcenterMember ucenterMember = memberService.getById(id);
        UcenterMemberOrder memeber = new UcenterMemberOrder();
        BeanUtils.copyProperties(ucenterMember,memeber);
        return memeber;
    }

    //查询某一天的注册人数
    @ApiOperation("查询某一天的注册人数 根据日期")
    @PostMapping("/register/{day}")
    @ResponseBody
    public R getDate(@PathVariable String day){
        System.out.println(day);

            //查询当天注册的用户
        int count = memberService.countDate(day);
        //封装返回数据
        return R.ok().data("count",count);
    }

    @PostMapping("/test")
    public R gettest(){
        return R.ok().message("测试");
    }

}

